package com.zefor.form;

import kd.bos.entity.datamodel.ListSelectedRow;
import kd.bos.entity.datamodel.ListSelectedRowCollection;
import kd.bos.form.control.events.ItemClickEvent;
import kd.bos.list.BillList;
import kd.bos.list.plugin.AbstractListPlugin;
import kd.bos.orm.query.QCP;
import kd.bos.orm.query.QFilter;
import kd.bos.servicehelper.BusinessDataServiceHelper;

import java.util.*;

import kd.bos.dataentity.entity.DynamicObject;

import kd.bos.servicehelper.operation.SaveServiceHelper;


public class WarnLogStatusChange extends AbstractListPlugin {

    @Override
    public void itemClick(ItemClickEvent evt) {
        super.itemClick(evt);
        //标记为当前记录已解决
        if("n716_baritemap".equals(evt.getItemKey())){
            changeStatus("已解决");
        }else if ("n716_baritemap_A".equals(evt.getItemKey())) {
            //重新标记为未解决
            changeStatus("待解决");
        }else if("n716_baritemap_B".equals(evt.getItemKey())){
            changeStatus("保留");
        }

    }

    /*
     * 改变记录状态
     * @param 状态
     * */
    private void changeStatus(String status) {
        ListSelectedRowCollection selectedRows = this.getSelectedRows();
        if (selectedRows.isEmpty()) {
            this.getView().showTipNotification("还未选择数据");
        }else {
            for (ListSelectedRow selectedRow : selectedRows) {
                DynamicObject dynamicObject = BusinessDataServiceHelper.loadSingle("n716_warn_log", "n716_combofield", new QFilter("id", QCP.equals, selectedRow.getPrimaryKeyValue()).toArray());
                dynamicObject.set("n716_combofield", status);
                SaveServiceHelper.save(new DynamicObject[]{dynamicObject});
            }
            ((BillList) this.getControl("billlistap")).refresh();
        }
    }

    /*
     * 注册点击事件
     */
    @Override
    public void registerListener(EventObject e) {
        super.registerListener(e);
        // 注册登录按钮
        this.addClickListeners("n716_baritemap");
        this.addClickListeners("billlistap");
    }

}
